import Vue from 'vue'
import VueRouter from 'vue-router'
import Film from '@/views/Film'
import Cinema from '@/views/Cinema'
import Center from '@/views/Center'
import Nowplaying from '@/views/Film/Nowplaying'
import Comingsoon from '@/views/Film/Comingsoon'
import Detail from '@/views/Detail'

Vue.use(VueRouter)// 注册路由模块

const routes = [
  {
    path: '/Film',
    component: Film,
    children: [
      {
        path: 'nowplaying',
        component: Nowplaying
      },
      {
        path: 'comingsoon',
        component: Comingsoon
      },
      {
        path: '',
        redirect: '/Film/nowplaying'
      }
    ]
  },
  {
    path: '/cinema',
    component: Cinema
  },
  {
    path: '/center',
    alias: '/wode', // 别名
    component: Center
  },
  // 动态路由配置
  {
    path: '/detail/:id',
    name: 'detailId', // 命名路由
    component: Detail
  },
  {
    path: '*',
    redirect: '/Film'
  }
  // 命名视图
  // {
  //   path: '/',
  //   components: {
  //     default: Foo,
  //     a: Bar,
  //     b: Baz
  //   }
  // }
]

const router = new VueRouter({
  mode: 'history',
  // base: process.env.BASE_URL,
  routes
})

const user = {
  isLogin () {
    return true
  }
}

// 全局的路由守卫(拦截)
router.beforeEach((to, from, next) => {
  if (to.path === '/center') {
    if (user.isLogin()) {
      next()
    } else {
      next('/login')
    }
  } else {
    next() // 不拦截 继续
  }
})

export default router
